<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                <span v-if="baTable.form.operate == 'Add'">{{ t('添加') }}</span>
                <span v-if="baTable.form.operate == 'Edit'">{{ t('编辑') }}</span>
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                >

                    <FormItem 
                        :label="t('产品分类')" 
                        type="remoteSelect" 
                        v-model="baTable.form.items!.group_id" 
                        :placeholder="t(' ')"
                        :input-attr="{ 
                            params: { isTree: true, treeAll: true }, 
                            pk: 'id', 
                            field: 'name', 
                            'remote-url': '/system/customer.Group/index?customer_id=' + baTable.form.items!.customer_id,
                        }" 
                    />
                    
                    <FormItem :label="t('产品名称')" type="string" v-model="baTable.form.items!.name" />
                    <el-form-item :label="t('购买币种')">
                        <el-radio-group v-model="baTable.form.items!.buy_currency">
                            <el-radio label="CNY" />
                            <el-radio label="KRW" />
                            <el-radio label="USD" />
                        </el-radio-group>
                    </el-form-item>
                    <FormItem :label="t('购买价格')" type="string" v-model="baTable.form.items!.buy_price" />
                    <el-form-item :label="t('销售币种')">
                        <el-radio-group v-model="baTable.form.items!.sale_currency">
                            <el-radio label="CNY" />
                            <el-radio label="KRW" />
                            <el-radio label="USD" />
                        </el-radio-group>
                    </el-form-item>
                    <FormItem :label="t('销售价格')" type="string" v-model="baTable.form.items!.sale_price" />
                    <FormItem 
                        :label="t('供货商')" 
                        type="remoteSelect" 
                        v-model="baTable.form.items!.supplier_id" 
                        :placeholder="t(' ')"
                        :input-attr="{ 
                            params: { isTree: true, treeAll: true }, 
                            pk: 'id', 
                            field: 'corp_name_cn', 
                            'remote-url': '/system/corp.Supplier/index',
                        }" 
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('取消') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('保存') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()


</script>

<style scoped lang="scss">
:deep(.el-upload-list--picture-card .el-upload-list__item){
    width: 90px;
    height: 90px;
}
:deep(.el-upload--picture-card){
    width: 90px;
    height: 90px;
}
:deep(.ba-upload-select-image){
    width: 90px;
}
:deep(.ba-upload.files .el-button [class*=el-icon]+span){
    position: relative;
    top: -3px;
}
</style>
